package theme

import (
	"fyne.io/fyne/v2"
	theme2 "fyne.io/fyne/v2/theme"
	"image/color"
	"io/ioutil"
)

type MyTheme struct {
	isDefault bool
	fontPath  string
}

var _ fyne.Theme = (*MyTheme)(nil)

func NewTheme(path string) fyne.Theme {
	isDefault := false
	if path == "" {
		isDefault = true
	}
	return &MyTheme{
		fontPath:  path,
		isDefault: isDefault,
	}
}
func (*MyTheme) Color(n fyne.ThemeColorName, v fyne.ThemeVariant) color.Color {
	return theme2.DefaultTheme().Color(n, v)
}

func (*MyTheme) Icon(n fyne.ThemeIconName) fyne.Resource {
	return theme2.DefaultTheme().Icon(n)
}

func (*MyTheme) Size(n fyne.ThemeSizeName) float32 {
	return theme2.DefaultTheme().Size(n)
}

func (t *MyTheme) Font(s fyne.TextStyle) fyne.Resource {
	if t.isDefault {
		return &theme2.PrimaryThemedResource{}
	} else {
		return fyne.NewStaticResource("MyFont", t.loadFont()) // 加载自定义字体
	}
}

func (t *MyTheme) loadFont() []byte {
	//fontPath := "./simhei.ttf" // 替换为你的字体路径
	fontBytes, err := ioutil.ReadFile(t.fontPath)
	if err != nil {
		panic(err) // 处理错误
	}
	return fontBytes
}
